{extend name="public/form" /}

{block name="body"}
	<form class="layui-form" action="">
		<div class="mainBox">
			<div class="main-container">
				<div class="layui-form-item">
					<label class="layui-form-label">账号</label>
					<div class="layui-input-block">
						<input type="text" name="name" lay-verify="title" autocomplete="off" placeholder="请输入用户名" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">邮箱</label>
					<div class="layui-input-block">
						<input type="text" name="email" lay-verify="email" autocomplete="off" placeholder="请输入邮箱" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">头像</label>
					<div class="layui-input-inline">
						<input type="text" name="user_img" lay-verify="required" placeholder="请上传图片" autocomplete="off" class="layui-input" >
					</div>
					<button style="float: left;" type="button" class="pear-btn" id="layuiadmin-upload-useradmin">上传图片</button> 
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">密码</label>
					<div class="layui-input-block">
						<input type="text" name="password" lay-verify="password" autocomplete="off" placeholder="请输入密码" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">手机</label>
					<div class="layui-input-block">
						<input type="text" name="phone" lay-verify="title" autocomplete="off" placeholder="请输入手机号" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">性别</label>
					<div class="layui-input-block">
						<input type="radio" name="sex" value="0" title="男">
						<input type="radio" name="sex" value="1" title="女" checked>
					</div>
				</div>
			</div>
		</div>
		<div class="bottom">
			<div class="button-container">
				<button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="user-save">
					<i class="layui-icon layui-icon-ok"></i>
					提交
				</button>
				<button type="reset" class="pear-btn pear-btn-sm">
					<i class="layui-icon layui-icon-refresh"></i>
					重置
				</button>
			</div>
		</div>
	</form>
	
	{__block__}

	<script>
		layui.use(['form', 'jquery', 'upload'], function() {
			let form = layui.form;
			let $ = layui.jquery;
			let upload = layui.upload;

			let ADD_URL = "{:url('user.user/add')}";

			form.on('submit(user-save)', function(data) {
				$.ajax({
					url: ADD_URL,
					data: JSON.stringify(data.field),
					dataType: 'json',
					contentType: 'application/json',
					type: 'post',
					success: function(result) {
						if (result.code === 0) {
							layer.msg(result.msg, {
								icon: 1,
								time: 1000
							}, function() {
								parent.layui.table.reload("user-table");
								parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
							});
						} else {
							layer.msg(result.msg, {
								icon: 2,
								time: 1000
							});
						}
					}
				})
				return false;
			});

			//上传头像
			upload.render({
				elem: '#layuiadmin-upload-useradmin'
				,url: "{:url('user.user/uploadImg')}"
				,data: {type:'image'}
				,accept: 'images'
				,method: 'get'
				,acceptMime: 'image/*'
				,done: function(res){
					$(this.item).prev("div").children("input").val(res.src);
					if(res.code === 0){
					layer.msg(res.msg,{
						icon:6,
						tiye:2000
						});
					} else {
						layer.open({
						title:"上传失败",
						content:res.msg,
						icon:5,
						anim:6
						});
					}
				}
			});
		})
	</script>

{/block}
